package com.bankcoder;


import com.alibaba.excel.EasyExcel;

import com.bankcoder.entity.CheckResultEntity;
import com.bankcoder.entity.CmpCheckEntity;
import com.bankcoder.entity.ResultModel;

import com.bankcoder.listener.GetCmpDataListener;
import com.bankcoder.operation.BusiUtil;
import com.bankcoder.operation.DoCheckRule;


import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class QualityManagementApplication {
    //读取外部配置文件
    public static final String confPath="D:\\filepath.properties";
    public static String cmpFilePath="";
    public static String cmpFileSheet="";
    //配置666文件夹
    public static String allFilesPath="";
    public static String getAllFilesPath() {
        return allFilesPath;
    }

    public static void setAllFilesPath(String allFilesPath) {
        QualityManagementApplication.allFilesPath = allFilesPath;
    }
    public static String targetPath="";
    public static String targetFileSheet="";

    public static void main(String[] args) throws IOException {
        //获取文件配置路径
        Properties properties = new Properties();
        try{
            /** 这个是加载类路径下的
             * properties.load(new QualityManagementApplication().getClass().getResourceAsStream("/filepath.properties"));
             */
            properties.load(new FileInputStream(confPath));
//            System.out.println(properties.getProperty("cmpFilePath"));
//            System.out.println(properties.getProperty("cmpFileSheet"));
//            System.out.println(properties.getProperty("targetPath"));
//            System.out.println(properties.getProperty("targetFileSheet"));
            //获取配置的路径和要读取的sheet页
            cmpFilePath=properties.getProperty("cmpFilePath");
            cmpFileSheet=properties.getProperty("cmpFileSheet");
            allFilesPath=properties.getProperty("allFilesPath");
            targetPath=properties.getProperty("targetPath");
            targetFileSheet=properties.getProperty("targetFileSheet");

        }catch (RuntimeException ex){
            throw new RuntimeException("未找到配置文件,请确认！"+ex.getCause());
        }

        //这里使用EasyExcel的reader进行读取数据
        GetCmpDataListener cmpDataListener=new GetCmpDataListener<CmpCheckEntity>();
        EasyExcel.read(cmpFilePath, CmpCheckEntity.class,cmpDataListener).sheet(cmpFileSheet).doRead();
        List<CmpCheckEntity> cmpDatalist = cmpDataListener.getAllCmpData();

        //初始化所有规则检查的结果集合
        List<ResultModel> allRestlts=new ArrayList<ResultModel>();
        DoCheckRule doCheck=new DoCheckRule();
        //查询回数据进行处理
        if(cmpDatalist !=null && cmpDatalist.size()>0){
            for(CmpCheckEntity cmpCheckEntity : cmpDatalist){
                //todo rule1:文件命名规则
                //需要返回来一个ResultModel集合，然后把所有检查项集合汇总了一起写到excel里
                System.out.println("文件命名规则检查开始");
                List<ResultModel> list1= doCheck.checkFileName(cmpCheckEntity);
                allRestlts.addAll(list1);
                System.out.println("文件命名规则检查完成");
                //todo rule2:主机梳理记录
                //检查规则：复杂度为高、中交易，需要400老师评审通过后，进入开发阶段
                //System.out.println("==>"+cmpCheckEntity.getProcessName()+"==="+cmpCheckEntity.getTranName()+"===="+cmpCheckEntity.getComplexLevel());
                if(!cmpCheckEntity.getComplexLevel().equals("低")){
                    //System.out.println("yangmlb########"+cmpCheckEntity.getProcessName()+"==="+cmpCheckEntity.getTranName()+"===="+cmpCheckEntity.getComplexLevel());
                    //根据程序名找到所有路径包括”评审记录“的文件，读取文件内容，判断其是否通过
                    System.out.println("主机评审规则检查开始");
                    list1=doCheck.checkHostReview(cmpCheckEntity);
                    allRestlts.addAll(list1);
                    System.out.println("主机评审规则检查结束");
                }
                //todo rule3:单元测试
                System.out.println("单元测试规则检查开始");
                list1=doCheck.checkUintTestResult(cmpCheckEntity);
                allRestlts.addAll(list1);
                System.out.println("单元测试规则检查结束");
            }

            //todo 处理序号
            allRestlts=BusiUtil.initResultId(allRestlts);
            //todo 待所有规则都检查完毕，最后执行写入
            EasyExcel.write(targetPath, CheckResultEntity.class).sheet(targetFileSheet).doWrite(allRestlts);
            System.out.println("文件已生成，请去约定的路径进行检查！");
        }
    }
}
